package com.boonya.lab.rpc.client;

import com.boonya.protobuf.grpc.HelloReply;
import com.boonya.protobuf.grpc.HelloRequest;
import com.boonya.protobuf.grpc.SimpleGrpc;
import io.grpc.StatusRuntimeException;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class GrpcClientService {

    //@GrpcClient("proto-server") // 这里拿不到nacos管理的实例，暂时采用手动配置
    @Qualifier("simpleBlockingStub")
    @Autowired
    private SimpleGrpc.SimpleBlockingStub simpleStub;

    public String sendMessage(final String name) {
        try {
            final HelloReply response = this.simpleStub.sayHello(HelloRequest.newBuilder().setName(name).build());
            return response.getMessage();
        } catch (final StatusRuntimeException e) {
            log.error("Request failed", e);
            return "FAILED with " + e.getStatus().getCode();
        }
    }
}
